/**
 * 
 */
package com.wxq.weitool;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author West.W
 * create date 2018-01-26 11:25:18
 */

public class Table<R,C,V> {
	private Map<R,Map<C,V>> table;
	private boolean isColumnSorted;
	public Table(boolean isRowSorted,boolean isColumnSorted) {
		if (isRowSorted) {
			table =new LinkedHashMap<>();
		} else {
			table =new HashMap<>();

		}
		
		this.isColumnSorted=isColumnSorted;
	}
	public static <R,C,V> Table<R,C,V> create(){
		return new Table<>(false, false);
		}
	
	/**添加值
	 * @param rowKey 行键
	 * @param columnKey 列键
	 * @param value 值
	 * @return 值
	 */
	public V put(R rowKey, C columnKey, V value) {
		if (table.containsKey(rowKey)) {
			table.get(rowKey).put(columnKey, value);
		} else {
			Map<C, V> row = createRow();
			row.put(columnKey, value);
			table.put(rowKey, row);
		}
		return value;
	}
	/**返回全部键值
	 * @return
	 */
	public Map<R, Map<C, V>> rowMap(){
		return table;
	}
	 /**获取值
	 * @param rowKey 行键
	 * @param columnKey 列键
	 * @return
	 */
	public V get(Object rowKey, Object columnKey) {
		 Map<C, V> row = table.get(rowKey);
		 if (row==null) {
			return null;
		} else {
			return row.get(columnKey);
		}
	 }
	private Map<C,V> createRow() {
		if (isColumnSorted) {
			return new LinkedHashMap<>();
		} else {
			return new HashMap<>();
			
		}
	}
}
